/**
 * 
 */
package org.xlab.baseframework.listener;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.xlab.baseframework.common.entity.AbstractSystemBean;
import org.xlab.baseframework.domain.ISystemUserDomain;
import org.xlab.baseframework.exception.RootUserNotFoundException;
import org.xlab.baseframework.exception.SystemCodeDuplicateException;
import org.xlab.baseframework.exception.SystemCodeNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.service.api.ISystemCodeService;
import org.xlab.baseframework.service.spi.ISystemInitializationListener;
import org.xlab.baseframework.system.PlatformDeclare;

/**
 * @author MaKuBEX
 * 
 */
@Component
public class LogTypeGenerater extends AbstractSystemBean implements
		ISystemInitializationListener {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 9172094627467235024L;
	private static final Logger log = Logger.getLogger(LogTypeGenerater.class);

	@Autowired(required = true)
	@Qualifier("bpf_systemCodeService")
	private ISystemCodeService systemCodeService;

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#getSequence()
	 */
	public int getSequence() {
		return PlatformDeclare.SEQUENCE_OF_SYSTEM_CODE_LOADER - 1;
	}

	/**
	 * @return the systemCodeService
	 */
	public ISystemCodeService getSystemCodeService() {
		return systemCodeService;
	}

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#onSystemStarted()
	 */
	public void onSystemStarted() throws SystemRuntimeException {
		log.debug("Listener [" + this.getClass().getName()
				+ "] is running......");
		ISystemUserDomain root = null;
		try {
			root = super.userService.getSystemRootUser();
			this.systemCodeService.findByCode(
					PlatformDeclare.CODEGROUP_OF_LOGTYPE, false);
		} catch (SystemCodeNotFoundException e) {
			log.debug("Now creating system code for log type");
			try {
				this.systemCodeService.createSystemCode(
						PlatformDeclare.CODEGROUP_OF_LOGTYPE,
						PlatformDeclare.DESC_OF_CODEGROUP_LOGTYPE, 0L, null, root);
			} catch (SystemCodeDuplicateException e1) {
				e1.printStackTrace();
			}
		} catch (SystemCodeDuplicateException e) {
			throw new SystemRuntimeException(e);
		} catch (RootUserNotFoundException e) {
			throw new SystemRuntimeException(e);
		}
		try {
			this.systemCodeService.findByCode(
					PlatformDeclare.CODE_OF_LOGTYPE_NORMAL, false);
		} catch (SystemCodeNotFoundException e) {
			log.debug("Now creating system code for log type normal");
			try {
				this.systemCodeService.createSystemCode(
						PlatformDeclare.CODE_OF_LOGTYPE_NORMAL,
						PlatformDeclare.DESC_OF_LOGTYPE_NORMAL, 1L,
						this.systemCodeService.findByCode(
								PlatformDeclare.CODEGROUP_OF_LOGTYPE, true),
						root);
			} catch (SystemCodeNotFoundException e1) {
				throw new SystemRuntimeException(e1);
			} catch (SystemCodeDuplicateException e1) {
				throw new SystemRuntimeException(e1);
			}
		} catch (SystemCodeDuplicateException e) {
			throw new SystemRuntimeException(e);
		}
		try {
			this.systemCodeService.findByCode(
					PlatformDeclare.CODE_OF_LOGTYPE_EXCEPTION, false);
		} catch (SystemCodeNotFoundException e) {
			log.debug("Now creating system code for log type exception");
			try {
				this.systemCodeService.createSystemCode(
						PlatformDeclare.CODE_OF_LOGTYPE_EXCEPTION,
						PlatformDeclare.DESC_OF_LOGTYPE_EXCEPTION, 2L,
						this.systemCodeService.findByCode(
								PlatformDeclare.CODEGROUP_OF_LOGTYPE, true),
						root);
			} catch (SystemCodeNotFoundException e1) {
				throw new SystemRuntimeException(e1);
			} catch (SystemCodeDuplicateException e1) {
				throw new SystemRuntimeException(e1);
			}
		} catch (SystemCodeDuplicateException e) {
			throw new SystemRuntimeException(e);
		}
	}

	/**
	 * @param systemCodeService
	 *            the systemCodeService to set
	 */
	public void setSystemCodeService(ISystemCodeService systemCodeService) {
		this.systemCodeService = systemCodeService;
	}

}
